var ExclusionList = {
    id: "exclusionListTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

ExclusionList.initColumn = function () {
    return [
        {visible: false},
        {title: '单病种', field: 'entityName', align: 'left', width:70, valign: 'middle', sortable: false},
        {title: '入单日期', field: 'inDateTime', align: 'left', width:110,valign: 'middle', sortable: false},
        {title: '病案号', field: 'mrNum', align: 'center', width:50, valign: 'middle', sortable: false},
        {title: '患者姓名', field: 'name', align: 'center', width:80, valign: 'middle', sortable: false},
        {title: '出院日期', field: 'dishDate', align: 'left', width:110,valign: 'middle', sortable: false},
        {title: '出院科室', field: 'admLocDesc', align: 'left', width:110,valign: 'middle', sortable: false},
        {title: '管理医生', field: 'admDoctorName', align: 'center', width:80,valign: 'middle', sortable: false},
        {title: '排除日期', field: 'operDate', align: 'center', width:110,valign: 'middle', sortable: false},
        {title: '排除医生', field: 'operUser', align: 'center', width:80,valign: 'middle', sortable: false},
        {title: '排除原因', field: 'operReason', align: 'left', width:180,valign: 'middle', sortable: false}
    ];
};

ExclusionList.search = function (){
    var startDate = $('#startDate').datebox('getValue');
    var endDate = $('#endDate').datebox('getValue');
    var queryData = {};
    queryData['startDate'] = startDate;    //获取页面开始时间
    queryData['endDate'] = endDate;    //获取页面结束时间
    queryData['office'] = $('#office').datebox('getValue');//部门
    queryData['depart'] = $('#depart').datebox('getValue');//科室
    queryData['entityType'] = $('#entityType').datebox('getValue');//患者类别
    var exclusion = $("#exclusion").datebox("getValue");
    if ("0" == exclusion){
        queryData['exclusion']  = ""
    }else if ("1" == exclusion){
        queryData['exclusion'] = "系统"
    }else if ("2" == exclusion){
        queryData['exclusion'] = "人工"
    }

    if(queryData['startDate']!=""){
        queryData['startDate'] = queryData['startDate'] + " 00:00:00";
    }
    if(queryData['endDate']!=""){
        queryData['endDate'] = queryData['endDate'] + " 23:59:59";
    }

    var longStart = Date.parse(startDate);
    var longEnd = Date.parse(endDate);
    if(longStart>longEnd){
        Sys.error("开始日期不可大于结束日期");
        return false;
    }
    ExclusionList.table.refresh({query: queryData,pageNumber: 1});
}

/**
 * //根据父级机构 查询子机构
 */
ExclusionList.officeChange = function(that){
    var ajax = new $ax(Sys.backendPath + "/sds/common/deptListByHospId", function(data){
        $('#depart').combobox({
            data:data,
            valueField:'value',
            textField:'text',
            defaultFilter: 6,
            onLoadSuccess: function () { //加载完成后,val[0]写死设置选中第一项
                $(this).combobox("setValue", "");
            }
        });
    },function(data){
        layer.msg("查询失败!" + data.responseJSON.message + "!",{icon: 2});
    });
    ajax.set({"hospID":that.value,"id":"depart"});
    ajax.start();
}


$(function () {
    //科室
    $('#depart').combobox({
        data:$.parseJSON($("#departMap").val()),
        valueField:'value',
        textField:'text'
    });
    //院区
    $('#office').combobox({
        data:$.parseJSON($("#officeMap").val()),
        valueField:'value',
        textField:'text',
        defaultFilter: 6,
        onSelect:ExclusionList.officeChange,
        onLoadSuccess: function () {
            var hospId = $('#hospitalId').val();
            if (hospId != "") {
                $(this).combobox("setValue", hospId);
                $(this).combobox('disable');
            } else {
                //加载完成后,val[0]写死设置选中第一项
                var val = $(this).combobox("getData");
                if (val.length == 2) {//如果就一个院区 加上全部=2
                    $(this).combobox("setValue", val[1].value);
                    ExclusionList.officeChange(val[1]);
                } else {
                    $(this).combobox("setValue", "");
                }
            }
        }
    });
    //病种
    $('#entityType').combobox({
        data:$.parseJSON($("#entityMap").val()),
        valueField:'value',
        textField:'text',
        defaultFilter: 6
    });
    //渲染datebox --没有渲染的话 图标左右间距不等
    $('.hisui-datebox').datebox({});
    $('#startDate').datebox('setValue', timeStamp2String(new Date().getTime()-(1000 * 60 * 60 * 24 * 30)));
    $('#endDate').datebox('setValue', timeStamp2String(new Date().getTime()));
    $('#exclusion').combobox({
        data:[
            {value:"0",text:"全部"},
            {value:"1",text:"系统排除"},
            {value:"2",text:"手工排除"}
        ],
        valueField:'value',
        textField:'text',
        onLoadSuccess:function () {
            $(this).combobox("setValue", "0");
        }
    });

    var startDate = $('#startDate').datebox('getValue');//开始日期
    var endDate = $('#endDate').datebox('getValue');//结束日期
    startDate = startDate + " 00:00:00";
    endDate = endDate + " 23:59:59";
    var entityType = $('#entityType').combobox('getValue');
    var exclusion = $("#exclusion").datebox("getValue");
    if ("0" == exclusion){
        exclusion  = ""
    }else if ("1" == exclusion){
        exclusion = "系统"
    }else if ("2" == exclusion){
        exclusion = "人工"
    }

    var defaultColunms = ExclusionList.initColumn();
    var table = new BSTable(ExclusionList.id, Sys.backendPath + "/analysis/exclusion/selectSdsExclusionTable", defaultColunms);
    table.setPaginationType("server");
    table.setQueryParams({entityType:entityType,startDate:startDate,endDate:endDate,office:$("#office").combobox("getValue"),depart:$("#depart").combobox("getValue"),exclusion:exclusion});
    table.setShowColumns(false);
    table.setShowRefresh(false);
    table.setShowToggle(false);
    table.setPageSize(15);
    table.setSubHeight(-27);
    // table.setResizable(true);

    ExclusionList.table = table.init();
    table.refresh();
});

function timeStamp2String(time){
    var datetime = new Date();
    datetime.setTime(time);
    var year = datetime.getFullYear();
    var month = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
    var date = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
    return year + "-" + month + "-" + date;
}